import java.util.LinkedList;
import java.util.List;
import java.util.Stack;

public class Solutiona {
    public class TreeNode {
     int val;
     TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
   TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
        public List<Integer> inorderTraversal(TreeNode root) {
            List<Integer> list=new LinkedList<>();
            if(root==null){
                return list;
            }
            Stack<TreeNode> stack=new Stack<>();
            TreeNode cur=root;
            TreeNode top=null;
            while(cur!=null||!stack.isEmpty()){
                while(cur!=null){
                    stack.push(cur);
                    cur=cur.left;
                }
                top=stack.pop();
                list.add(top.val);
                cur=top.right;
            }
            return list;
        }
    public List<Integer> postorderTraversal(TreeNode root) {
        List<Integer> list=new LinkedList<>();
        if(root==null){
            return list;
        }
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        TreeNode top=null;
        TreeNode prev=null;
        while(cur!=null||!stack.isEmpty()){
            while(cur!=null){
                stack.push(cur);
                cur=cur.left;
            }
            top=stack.peek();
            if(top.right==null||top.right==prev){
                top=stack.pop();
                list.add(top.val);
                prev=top;
            }else{
                cur=top.right;
            }
        }
        return list;
    }
    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list=new LinkedList<>();
        if(root==null){
            return list;
        }
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        TreeNode top=null;
        while(cur!=null||!stack.isEmpty()){
            while(cur!=null){
                stack.push(cur);
                list.add(cur.val);
                cur=cur.left;
            }
            top=stack.pop();
            cur=top.right;
        }
        return list;
    }

    public int i=0;
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return buildTreeChild(preorder,inorder,0,inorder.length-1);
    }
    public TreeNode buildTreeChild(int[] preorder,int[] inorder,int left,int right) {
        if(left>right){
            return null;
        }
        TreeNode root=new TreeNode(preorder[i]);
        int rootIndex=findIndex(inorder,left,right,preorder[i]);
        i++;
        root.left=buildTreeChild(preorder,inorder,left,rootIndex-1);
        root.right=buildTreeChild(preorder,inorder,rootIndex+1,right);
        return root;
    }
    public int findIndex(int[] inorder,int left,int right,int key){
        for(int j=left;j<=right;j++){
            if(inorder[j]==key){
                return j;
            }
        }
        return-1;
    }

//    public int i=0;
//    public TreeNode buildTree(int[] inorder, int[] postorder) {
//        i=postorder.length-1;
//        return buildTreeChild(inorder, postorder,0,inorder.length-1);
//    }
//    public TreeNode  buildTreeChild(int[] inorder,int[] postorder,int left,int right){
//        if(left>right){
//            return null;
//        }
//        TreeNode root=new TreeNode(postorder[i]);
//        int posIndex=findIndex(inorder,left,right,postorder[i]);
//        i--;
//        root.right=buildTreeChild(inorder, postorder,posIndex+1,right);
//        root.left=buildTreeChild(inorder, postorder,left,posIndex-1);
//        return root;
//    }
//    public int findIndex(int[] inorder,int left,int right,int key){
//        for(int j=left;j<=right;j++){
//            if(inorder[j]==key){
//                return j;
//            }
//        }
//        return-1;
//    }
    }

